package cn.tedu.charging.order.config;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ 配置类
 */
@Configuration
public class RabbitMQConfiguration {

    /**
     * 队列名称
     */
    public static final String QUEUE_NAME = "charging-order-queue";

    /**
     * 交换机名称   暂时不深究
     */
    public static final String EXCHANGE_NAME = "charging-order-exchange";

    /**
     * 路由键名称
     */
    public static final String ROUTING_KEY = "charging-order-routing-key";

    /**
     * 声明一个队列 用来存储数据
     *
     */
    @Bean
    public Queue queue() {
        //是否持久化
        boolean durable = true;

        return new Queue(QUEUE_NAME, durable);
    }

    /**
     * 创建一个交换机
     */
    @Bean
    public DirectExchange exchange() {
        //是否持久化
        boolean durable = true;
        //是否自动删除
        boolean autoDelete = false;
        return new DirectExchange(EXCHANGE_NAME, durable, autoDelete);
    }

    /**
     * 绑定队列和交换机 通过 路由键
     * @return
     */
    @Bean
    public Binding orderBinding() {
        return BindingBuilder.bind(queue()).to(exchange()).with(ROUTING_KEY);
    }

}